package com.ruoyi.web.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRegion;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 区域字典Controller
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@Api(tags="区域字典控制器")
@RestController
@RequestMapping("/common/region" )
public class SysRegionController extends BaseController {
    /**
     * 配置表别名 默认空
     */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private ISysRegionService sysRegionService;

    /**
     * 查询区域字典列表
     */
    @ApiOperation(value = "查询区域字典",notes = "区域字典管理")
    @PreAuthorize("@ss.hasPermi('common:region:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(SysRegion sysRegion,QueryWrapper<SysRegion> queryWrapper) {
        setQueryCondition(sysRegion,queryWrapper);
        startPage();
        List<SysRegion> list = sysRegionService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(SysRegion sysRegion, QueryWrapper<SysRegion> queryWrapper) {
        if(StrUtil.isNotBlank(sysRegion.getRegionName())){
            queryWrapper.like(TABLE_PREFIX+"region_name" ,sysRegion.getRegionName());
        }
        if(StrUtil.isNotBlank(sysRegion.getRegionCode())){
            queryWrapper.eq(TABLE_PREFIX+"region_code" ,sysRegion.getRegionCode());
        }
        if(StrUtil.isNotBlank(sysRegion.getCreaterName())){
            queryWrapper.like(TABLE_PREFIX+"creater_name" ,sysRegion.getCreaterName());
        }
        if(sysRegion.getDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"deleted" ,sysRegion.getDeleted());
        }
        if(StrUtil.isNotBlank(sysRegion.getVersion())){
            queryWrapper.eq(TABLE_PREFIX+"version" ,sysRegion.getVersion());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出区域字典列表
     */
    @PreAuthorize("@ss.hasPermi('common:region:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, SysRegion sysRegion,QueryWrapper<SysRegion> queryWrapper) {
        if (CollUtil.isEmpty( sysRegion.getIds())){
            setQueryCondition(sysRegion,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",sysRegion.getIds());
        }
        List<SysRegion> list = sysRegionService.list(queryWrapper);
        ExcelUtil<SysRegion> util = new ExcelUtil<SysRegion>(SysRegion. class);
        util.exportExcel(response,list, "region");
    }


    /*
    * 获取子项数据
    * */
    @GetMapping("/getChildrenByParentId/{id}")
    @Anonymous
    public AjaxResult getChildrenByParentId(@PathVariable("id") String id){
        return AjaxResult.success(sysRegionService.queryChildrenByParentId(id));
    }

    /**
     * 获取区域字典详细信息
     */
    @ApiOperation(value = "获取区域字典",notes = "区域字典管理")
    @PreAuthorize("@ss.hasPermi('common:region:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(sysRegionService.queryGetById(id));
    }


    @GetMapping("/nodeList")
    public AjaxResult getnodeList(){
        return AjaxResult.success(sysRegionService.getNodeTree());
    }


    /**
     * 获取区域字典详细信息根据code
     */
    @ApiOperation(value = "获取区域字典",notes = "区域字典管理")
    @GetMapping(value = "/code/{code}" )
    public AjaxResult getInfoByCode(@PathVariable("code" ) String code) {
        return AjaxResult.success(sysRegionService.getOne(new QueryWrapper<SysRegion>().eq("region_code",code)));
    }


    /**
     * 新增区域字典
     */
    @ApiOperation(value = "新增区域字典",notes = "区域字典管理")
    @PreAuthorize("@ss.hasPermi('common:region:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysRegion sysRegion) {
        return toAjax(sysRegionService.addSysRegion(sysRegion) ? 1 : 0);
    }

    /**
     * 修改区域字典
     */
    @ApiOperation(value = "修改区域字典",notes = "区域字典管理")
    @PreAuthorize("@ss.hasPermi('common:region:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody SysRegion sysRegion) {
        return toAjax(sysRegionService.updateSysRegion(sysRegion) ? 1 : 0);
    }

    /**
     * 删除区域字典
     */
    @ApiOperation(value = "删除区域字典",notes = "区域字典管理")
    @PreAuthorize("@ss.hasPermi('common:region:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysRegionService.deleteSysRegionByIds(ids) ? 1 : 0);
    }
}
